package com.sita.tboard.hitchhike.history;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.HitchhikeContentActivity;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.hitchhike.listeners.OnGetTicketsListener;
import com.sita.tboard.hitchhike.helper.HitchhikeNetworkHelper;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.trinea.android.common.util.ToastUtils;

public class DriverHistoryTicketsActivity extends HitchhikeContentActivity implements SwipeRefreshLayout.OnRefreshListener, OnMoreListener {

    @Bind(R.id.ticket_list)
    SuperRecyclerView listView;

    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    private DriverHistoryTicketsAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private int currPage = 0;

    public DriverHistoryTicketsActivity() {
    }

    @SuppressWarnings("unused")
    public static Intent newIntent() {
        Intent intent = new Intent(GlobalContext.getGlobalContext(), DriverHistoryTicketsActivity.class);
        return intent;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hitchhike_driver_history_trips);
        ButterKnife.bind(this);

        customizeToolbar(mToolbar, R.string.history_plan);

        mLinearLayoutManager = new LinearLayoutManager(this);
        listView.setLayoutManager(mLinearLayoutManager);

        mAdapter = new DriverHistoryTicketsAdapter(this);
        listView.setAdapter(mAdapter);

        listView.setupMoreListener(this, 0);
        listView.setRefreshListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (currPage == 0){
            requestData();
        }
    }

    @Override
    public void onRefresh() {
        currPage = 0;
        requestData();
    }

    @Override
    public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
        currPage += 1;
        requestData();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void requestData(){
        HitchhikeNetworkHelper.INSTANCE.getDriverHistoryTickets(currPage, 10, new OnGetTicketsListener() {
            @Override
            public void onSuccess(ArrayList<TicketModel> items) {
                if (currPage == 0) {
                    mLinearLayoutManager.scrollToPosition(0);
                    mAdapter.setData(items);
                    listView.setRefreshing(false);
                } else {
                    listView.setLoadingMore(false);
                    if (items != null && !items.isEmpty()) {
                        mAdapter.append(items);
                    } else {
                        currPage -= 1;// do not add currPage since nothing was retrieved
                    }
                }
            }

            @Override
            public void onFailure(Throwable throwable) {
                if (currPage > 0) {
                    currPage -= 1;// do not add currPage since nothing was retrieved
                }
                ToastUtils.show(GlobalContext.getGlobalContext(), com.sita.tboard.util.TextUtils.getThrowableMessage(throwable));
            }
        });
    }

}
